package com.bbms.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

import com.bbms.util.StringUtil;

public class SessionFilter extends OncePerRequestFilter {

	/* (non-Javadoc)
	 * @see org.springframework.web.filter.OncePerRequestFilter#doFilterInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		
		if (StringUtil.isNotEmpty((String)request.getSession().getAttribute("user"))){
			filterChain.doFilter(request, response);
		} else {
			String uri = request.getRequestURI();
			if (uri.indexOf("bbms") != -1){
				System.out.println(request.getContextPath());
				String name = request.getContextPath();
				response.sendRedirect(name + "/");
			} else {
				filterChain.doFilter(request, response);
			}
		}
	}

}
